/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sunspotworld;

import com.sun.spot.sensorboard.EDemoBoard;
import com.sun.spot.sensorboard.io.IIOPin;
import com.sun.spot.sensorboard.peripheral.Servo;

/**
 *
 * @author Administrator
 */
public class SonarServo extends Thread {
    
    
    EDemoBoard demo = EDemoBoard.getInstance();
    Servo sonarServo; 
    int servoPin;
    float orientation = 0.40f;
    float servoTurn = +0.02f;
    int counter = 0;
    
    public SonarServo( int servoPinIn ){
        servoPin = servoPinIn;
        sonarServo = new Servo(demo.getIOPins()[servoPin]);
        sonarServo.setBounds(500, 2100);
        setOrientation(0.5f);
    }
    
    public void run() {
        while(true){
                setOrientation( orientation );
                counter = 0;
                orientation += servoTurn;
            if( orientation >= 0.70f )
                servoTurn = -0.02f;
            if( orientation <= 0.30f )
                servoTurn = 0.02f;
            //}
            counter++;


            try {
                Thread.sleep(150);
            } catch (InterruptedException e) {
                //We've been interrupted: exit the thread
                return;
            }
        }
    }
    
    public void setOrientation( float orientation ) {
        sonarServo.setPosition( orientation );
    }

}
